﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace BackgroundTest.BasicAbstracts
{
    /************************************************************************/
    /* Class name: GUIObjects                                               */
    /* Function: The abstract of all drawable units on a battle field       */
    /************************************************************************/
    abstract public class GUIObjects : IDisposable
    {

        // The texture file that the GUI object possesses.
        protected Texture2D texture;
        public Texture2D GUITexture
        {
            get { return texture; }
        }

        // The color data of the Texture
        protected Color[] colordata;

        // The position for render.
        public VectorScreen position { get; set; }

        // The camera which controls the sight of the object
        protected Camera camera;

        // The rectangle position for render, automatically derive.
        public Rectangle rectanglePosition { get; set; }

        // The depth buffer (z-buffer).
        public float zBuffer { get; set; }

        // The integer that remembers the last frame's gametime.
        public int timeSinceLastFrame { get; set; }

        // Construction function.
        public GUIObjects ( Camera _camera ) 
        {
            camera = _camera;
            position = new VectorScreen(0, 0);
        }

        // Update the position of the object
        public virtual void UpdatePosition(GameTime gametime) { }

        // Using GUIBatch to render the object.
        public virtual void Render(SpriteBatch GUIBatch) { }

        // Used to free this class automatically.
        public void Dispose() { }

        /************************************************************************/
        /* Method name: LoadTexture                                             */
        /* Parameters: VectorScreen*2: the 2 positions to be cancelled.         */
        /************************************************************************/
        public void LoadTexture(Texture2D _texture)
        {
            texture = _texture;
            colordata = new Color[texture.Height * texture.Width];
            texture.GetData<Color>(colordata);
        }

        // Used to test is the point has collided with the GUIObject
        public bool Collided(VectorScreen _vs)
        {
            VectorScreen vs_unit = _vs + camera.position - position;
            if (vs_unit.x-texture.Width>0||vs_unit.y-texture.Height>0||vs_unit.x<=0||vs_unit.y<=0)
            {
                return false;
            }
            else
            {
                if (colordata[(vs_unit.y-1) * texture.Width + vs_unit.x-1].A != 0)
                {
                    return true;
                }
                else return false;
            }
        }
    }
}
